package me.miencraft.plugin.superlobby.util;

import me.miencraft.plugin.superlobby.SuperLobby;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.Set;

/**
 * @author langle__
 * @version 1.0
 */
public class VisiblePlayer {

    private static final Set<Player> VISIBLE_SET = new HashSet<>();

    public static void hidePlayerFromVisible(Player player) {
        for (Player target : VISIBLE_SET) {
            target.hidePlayer(SuperLobby.instance, player);
            player.hidePlayer(SuperLobby.instance, target);
        }
    }

    public static void toggleVisibility(@NotNull Player player) {
        if (isVisible(player)) {
            disableVisibility(player);
        } else {
            enableVisibility(player);
        }
    }

    public static void enableVisibility(@NotNull Player player) {
        addVisible(player);
        for (Player target : Bukkit.getOnlinePlayers()) {
            if (target.getUniqueId().equals(player.getUniqueId())) continue;
            target.hidePlayer(SuperLobby.instance, player);
            player.hidePlayer(SuperLobby.instance, target);
        }
    }

    public static void disableVisibility(@NotNull Player player) {
        for (Player target : Bukkit.getOnlinePlayers()) {
            if (isVisible(target)) continue;
            player.showPlayer(SuperLobby.instance, target);
            target.showPlayer(SuperLobby.instance, player);
        }
        removeVisible(player);
    }

    @Deprecated
    public static void addVisible(@NotNull Player player) {
        VISIBLE_SET.add(player);
    }

    @Deprecated
    public static void removeVisible(@NotNull Player player) {
        VISIBLE_SET.remove(player);
    }

    public static boolean isVisible(@NotNull Player player) {
        return VISIBLE_SET.contains(player);
    }

}
